﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace HITs
{
    public partial class Chart_Market : Form
    {
        const int soNgayHienThi = 100;
        const int step = 1;
        CoPhieu CP_ve;
        public Chart_Market()
        {
            InitializeComponent();
        }

        private void Chart_Market_Load(object sender, EventArgs e)
        {
            Calculate_RSI(0);
            Calculate_MFI(0);
            ///Calculate_Zscore(0);
            Calculate_IMI(0);
            CalculateMACD_BollingerBand();
            //Calculate_ChechLechZscore(soNgayHienThi);
        }

        private void Calculate_RSI(int soNgayCachHienTai)
        {
            int count_morethan70 = 0, count_lessthan30 = 0, count_05 = 0, count_other = 0;
            float score;
            foreach (CoPhieu cp in ShareData.danhsachCoPhieu.DanhSachCoPhieu_List)
            {
                if (cp.DanhSachNgayGiaoDich.Count > 100)
                {
                    score = cp.TinhDanhSachChiSo_RelativeStrengIndex_RSI(20, soNgayCachHienTai);
                    if (score >= 70f) count_morethan70++;
                    else if (score >= 50f) count_05++;
                    else if (score <= 30f) count_lessthan30++;
                    else count_other++;
                }
            }

            chart_RSI.Series.Clear();
            chart_RSI.ChartAreas[0].Area3DStyle.Enable3D = true;    

            chart_RSI.Series.Add("RSI");
            chart_RSI.Series[chart_RSI.Series.Count - 1].IsVisibleInLegend = false;
            chart_RSI.Series[chart_RSI.Series.Count - 1].Label = "#VALX\n#PERCENT";
            chart_RSI.Series[chart_RSI.Series.Count - 1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Doughnut;

            chart_RSI.Series[chart_RSI.Series.Count - 1].Points.AddXY(">=70%", count_morethan70);
            chart_RSI.Series[chart_RSI.Series.Count - 1].Points.AddXY(">=50%", count_05);
            chart_RSI.Series[chart_RSI.Series.Count - 1].Points.AddXY("<50%", count_other);
            chart_RSI.Series[chart_RSI.Series.Count - 1].Points.AddXY("<=30%", count_lessthan30);          
        }

        private void Calculate_IMI(int soNgayCachHienTai)
        {
            int count_morethan70 = 0, count_lessthan30 = 0, count_05 = 0, count_other = 0;
            float score;
            foreach (CoPhieu cp in ShareData.danhsachCoPhieu.DanhSachCoPhieu_List)
            {
                if (cp.DanhSachNgayGiaoDich.Count > 100)
                {
                    score = cp.TinhDanhSachChiSo_IntradayMomentumIndex_IMI(14, soNgayCachHienTai);
                    if (score >= 70f) count_morethan70++;
                    else if (score >= 50f) count_05++;
                    else if (score <= 30f) count_lessthan30++;
                    else count_other++;
                }
            }

            chart_Zscore.Series.Clear();
            chart_Zscore.ChartAreas[0].Area3DStyle.Enable3D = true;

            chart_Zscore.Series.Add("IMI");
            chart_Zscore.Series[chart_Zscore.Series.Count - 1].IsVisibleInLegend = false;
            chart_Zscore.Series[chart_Zscore.Series.Count - 1].Label = "#VALX\n#PERCENT";
            chart_Zscore.Series[chart_Zscore.Series.Count - 1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Doughnut;

            chart_Zscore.Series[chart_Zscore.Series.Count - 1].Points.AddXY(">=70%", count_morethan70);
            chart_Zscore.Series[chart_Zscore.Series.Count - 1].Points.AddXY(">=50%", count_05);
            chart_Zscore.Series[chart_Zscore.Series.Count - 1].Points.AddXY("<50%", count_other);
            chart_Zscore.Series[chart_Zscore.Series.Count - 1].Points.AddXY("<=30%", count_lessthan30);
        }

        private void Calculate_MFI(int soNgayCachHienTai)
        {
            int count_morethan80 = 0, count_lessthan20 = 0, count_05 = 0, count_other = 0;
            float score;
            foreach (CoPhieu cp in ShareData.danhsachCoPhieu.DanhSachCoPhieu_List)
            {
                if (cp.DanhSachNgayGiaoDich.Count > 100)
                {
                    score = cp.TinhDanhSachChiSo_MoneyFlowIndex_MFI(14, soNgayCachHienTai);
                    if (score >= 80f) count_morethan80++;
                    else if (score >= 50f) count_05++;
                    else if (score <= 20f) count_lessthan20++;
                    else count_other++;
                }
            }

            chart_MFI.Series.Clear();
            chart_MFI.ChartAreas[0].Area3DStyle.Enable3D = true;            

            chart_MFI.Series.Add("MFI");
            chart_MFI.Series[chart_MFI.Series.Count - 1].IsVisibleInLegend = false;
            chart_MFI.Series[chart_MFI.Series.Count - 1].Label = "#VALX\n#PERCENT";
            chart_MFI.Series[chart_MFI.Series.Count - 1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Doughnut; 
       
            chart_MFI.Series[chart_MFI.Series.Count - 1].Points.AddXY(">=80%", count_morethan80);
            chart_MFI.Series[chart_MFI.Series.Count - 1].Points.AddXY(">=50%", count_05);
            chart_MFI.Series[chart_MFI.Series.Count - 1].Points.AddXY("<50%", count_other);
            chart_MFI.Series[chart_MFI.Series.Count - 1].Points.AddXY("<=20%", count_lessthan20);
        }

        //private void Calculate_Zscore(int soNgayCachHienTai)
        //{
        //    #region Tính toán
        //    List<float> listResult = new List<float>();
        //    int n = 3;
        //    for (int i = 0; i < 6; i++)
        //        listResult.Add(0f);

        //    List<int> listTopIndex_Zscore = ShareData.danhsachCoPhieu.Xuly_LayIndexTopNZScore(n, soNgayCachHienTai);
        //    List<CoPhieu> DanhSachCoPhieu = ShareData.danhsachCoPhieu.DanhSachCoPhieu_List;

        //    lbZscore.Text = "Zscore: ";
        //    foreach (int id in listTopIndex_Zscore)
        //    {
        //        lbZscore.Text += DanhSachCoPhieu[id].TenCoPhieu + "  ";
        //    }

        //    for (int i = 0; i < n; i++)
        //    {
        //        listResult[0] += DanhSachCoPhieu[listTopIndex_Zscore[i]].Xuly_TangTruongGia(soNgayCachHienTai, 3);
        //        listResult[1] += DanhSachCoPhieu[listTopIndex_Zscore[i]].Xuly_TangTruongGia(soNgayCachHienTai, 5);
        //        listResult[2] += DanhSachCoPhieu[listTopIndex_Zscore[i]].Xuly_TangTruongGia(soNgayCachHienTai, 8);
        //    }

        //    for (int i = 0; i < n; i++)
        //        listResult[i] = listResult[i] / n;

        //    List<int> listNoneZscore = new List<int>();

        //    for (int i = 0; i < DanhSachCoPhieu.Count; i++)
        //        if (listTopIndex_Zscore.Contains(i) == false)
        //        {
        //            if (DanhSachCoPhieu[i].DanhsachZscore.Count <= soNgayCachHienTai + 8)
        //                listNoneZscore.Add(i);
        //            else
        //            {
        //                listResult[3] += DanhSachCoPhieu[i].Xuly_TangTruongGia(soNgayCachHienTai, 3);
        //                listResult[4] += DanhSachCoPhieu[i].Xuly_TangTruongGia(soNgayCachHienTai, 5);
        //                listResult[5] += DanhSachCoPhieu[i].Xuly_TangTruongGia(soNgayCachHienTai, 8);
        //            }
        //        }

        //    listResult[3] = listResult[3] / (DanhSachCoPhieu.Count - n - listNoneZscore.Count);
        //    listResult[4] = listResult[4] / (DanhSachCoPhieu.Count - n - listNoneZscore.Count);
        //    listResult[5] = listResult[5] / (DanhSachCoPhieu.Count - n - listNoneZscore.Count);

        //    for (int i = 0; i < 6; i++)
        //        listResult[i] = (float)Math.Round(listResult[i], 2);
        //    #endregion

        //    #region Vẽ Chart
        //    chart_Zscore.Series.Clear();
            
        //    int[] T = new int[] { 3, 5, 8 };

        //    chart_Zscore.ChartAreas[0].AxisX.Interval = 1;
        //    chart_Zscore.ChartAreas[0].AxisY.IsStartedFromZero = false;

        //    for (int i = 0; i < n; i++)
        //    {
        //        chart_Zscore.Series.Add("Top T" + T[i]);
        //        chart_Zscore.Series[chart_Zscore.Series.Count - 1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
        //        chart_Zscore.Series[chart_Zscore.Series.Count - 1].ToolTip = "#VALX";
        //        chart_Zscore.Series[chart_Zscore.Series.Count - 1].Points.AddXY(i, listResult[i]);
             
        //        chart_Zscore.Series[chart_Zscore.Series.Count - 1].Color = Color.Red;
        //        chart_Zscore.Series[chart_Zscore.Series.Count - 1].BorderWidth = 5;
        //    }

        //    for (int i = 0; i < n; i++)
        //    {
        //        chart_Zscore.Series.Add("T" + T[i]);
        //        chart_Zscore.Series[chart_Zscore.Series.Count - 1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
        //        chart_Zscore.Series[chart_Zscore.Series.Count - 1].Points.AddXY(i + 3, listResult[i + 3]);
        //        chart_Zscore.Series[chart_Zscore.Series.Count - 1].Color = Color.Blue;
        //        chart_Zscore.Series[chart_Zscore.Series.Count - 1].BorderWidth = 5;
        //    }
        //    #endregion
        //}

        private void Calculate_ChechLechZscore(int soNgay)
        {
            #region Tính toán
            List<CoPhieu> DanhSachCoPhieu = ShareData.danhsachCoPhieu.DanhSachCoPhieu_List;
            List<List<float>> listValue = new List<List<float>>();
            float avg;
            int[] soNgayCach = new int[] { 7, 13, 21 };
            int n = 3;

            foreach(int id in soNgayCach)
                listValue.Add(new List<float>());

            for (int i = 0; i < soNgay; i++)
            {
                List<int> listTopIndex_Zscore = ShareData.danhsachCoPhieu.Xuly_LayIndexTopNZScore(n, i);

                for (int j = 0; j < soNgayCach.Length; j++)
                {
                    avg = 0f;
                    foreach (int id in listTopIndex_Zscore)
                        avg += DanhSachCoPhieu[id].Xuly_TangTruongGia(soNgayCach[j], 3);
                    avg = (float)Math.Round(avg / n, 1);
                    listValue[j].Add(avg);
                }
            }
            #endregion

            #region Vẽ Chart
            //chart_khoiluong.Series.Clear();
            //chart_khoiluong.ChartAreas[0].AxisX.Enabled = AxisEnabled.False;

            //for(int i = 0 ; i < soNgayCach.Length;i++)
            //{
            //    chart_khoiluong.Series.Add("T" + soNgayCach[i]);
            //    chart_khoiluong.Series[chart_khoiluong.Series.Count - 1].ChartType = SeriesChartType.Spline;
            //    chart_khoiluong.Series[chart_khoiluong.Series.Count - 1].BorderWidth = 3;
            //    chart_khoiluong.Series[chart_khoiluong.Series.Count - 1].Points.DataBindY(listValue[i]);
            //}

            //chart_khoiluong.Series.Add("Ngày");
            //for (int i = 0; i < soNgayHienThi; i++)
            //{
            //    if (i == (trackBar.Value * step))
            //        chart_khoiluong.Series[chart_khoiluong.Series.Count - 1].Points.AddY(10);
            //    else
            //        chart_khoiluong.Series[chart_khoiluong.Series.Count - 1].Points.AddY(0);
            //}
            #endregion
        }

        private void CalculateMACD_BollingerBand()
        {
            #region Tính toán
            List<float> listData = new List<float>();
            List<float> listVn30 = new List<float>();

            //List<float> XanhVoDoLong = new List<float>();
            //List<float> DoVoXanhLong = new List<float>();

            //ShareData.danhsachCoPhieu.Testing_HienTuong_XanhVoDoLong(ref XanhVoDoLong, ref DoVoXanhLong, 0.8f);

            CoPhieu cp_Temp = null;
            CoPhieu cp_vn30 = null;

            foreach (CoPhieu cp in ShareData.danhsachCoPhieu.DanhSachCoPhieu_List)
                if (cp.TenCoPhieu.ToLower() == "^vnindex")
                {
                    listData = cp.DanhSachGiaDongCua;
                    cp_Temp = cp;
                    CP_ve = cp;
                }
                //else if (cp.TenCoPhieu.ToLower() == "^vn30")
                //{
                //    listVn30= cp.DanhSachGiaDongCua;
                //    cp_vn30 = cp;
                //}
         

            //List<float> MACD = new List<float>();
            //List<float> signalLine = new List<float>();
            //List<float> listChechLech = new List<float>();
            //Indicators.MACD(listData, 12, 26, 9, ref MACD, ref signalLine);

            List<float> BB_tren = new List<float>();
            List<float> BB_duoi = new List<float>();
            List<float> BB_giua = new List<float>();
            Indicators.BollingerBand(listData, 20, ref BB_giua, ref BB_tren, ref BB_duoi);

            List<float> EMA_KhoiLuong = new List<float>();
            EMA_KhoiLuong = Indicators.EMA(cp_Temp.DanhSachKhoiLuongGiaoDich, 15);

            int chuky1 = 5, chuky2 = 13;
            List<float> EMA_5 = new List<float>();
            EMA_5 = Indicators.EMA(cp_Temp.DanhSachGiaDongCua, chuky1);
            List<float> EMA_10 = new List<float>();
            EMA_10 = Indicators.EMA(cp_Temp.DanhSachGiaDongCua, chuky2);
            #endregion

            #region Chart_giá
            chart_gia.Series.Clear();

            chart_gia.ChartAreas[0].AxisX.Enabled = AxisEnabled.False;
            chart_gia.ChartAreas[0].AxisY2.Enabled = AxisEnabled.False;
            chart_gia.ChartAreas[0].AxisY.LineColor = Color.Silver;
            chart_gia.ChartAreas[0].AxisY.LineDashStyle = ChartDashStyle.Dot;

            chart_gia.ChartAreas[0].AxisY2.LineColor = Color.Silver;
            chart_gia.ChartAreas[0].AxisY2.LineDashStyle = ChartDashStyle.Dot;

            chart_gia.ChartAreas[0].BorderDashStyle = ChartDashStyle.DashDotDot;
            chart_gia.ChartAreas[0].IsSameFontSizeForAllAxes = true;
            chart_gia.ChartAreas[0].AxisY.IsStartedFromZero = false;

            chart_gia.Series.Add("Giá");
            chart_gia.Series[chart_gia.Series.Count - 1].ChartType = SeriesChartType.Spline;
            chart_gia.Series[chart_gia.Series.Count - 1].BorderWidth = 3;
            chart_gia.Series[chart_gia.Series.Count - 1].Color = Color.Blue;
            chart_gia.Series[chart_gia.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(listData, soNgayHienThi));
            chart_gia.Series[chart_gia.Series.Count - 1].LabelBorderDashStyle = ChartDashStyle.DashDotDot;

            #region Testing - code
            /*
            //chart_gia.Series.Add("Ngày");
            //for (int i = 0; i < soNgayHienThi; i++)
            //{
            //    if (i == (trackBar.Value * step))
            //        chart_gia.Series[chart_gia.Series.Count - 1].Points.AddY(listData[i]);
            //    else
            //        chart_gia.Series[chart_gia.Series.Count - 1].Points.AddY(430);
            //}
             
            //chart_gia.Series.Add("Vn30");
            //chart_gia.Series[chart_gia.Series.Count - 1].ChartType = SeriesChartType.Spline;
            //chart_gia.Series[chart_gia.Series.Count - 1].BorderWidth = 3;
            //chart_gia.Series[chart_gia.Series.Count - 1].Color = Color.Red;
            //chart_gia.Series[chart_gia.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(listVn30, soNgayHienThi));
            //chart_gia.Series[chart_gia.Series.Count - 1].LabelBorderDashStyle = ChartDashStyle.DashDotDot;

            //chart_gia.Series.Add("MACD");
            //chart_gia.Series[chart_gia.Series.Count - 1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
            //chart_gia.Series[chart_gia.Series.Count - 1].BorderWidth = 2;
            //chart_gia.Series[chart_gia.Series.Count - 1].Color = Color.GreenYellow;
            //chart_gia.Series[chart_gia.Series.Count - 1].YAxisType = AxisType.Secondary;
            //chart_gia.Series[chart_gia.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(MACD, soNgayHienThi));

            //chart_gia.Series.Add("Signal Line");
            //chart_gia.Series[chart_gia.Series.Count - 1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
            //chart_gia.Series[chart_gia.Series.Count - 1].BorderWidth = 2;
            //chart_gia.Series[chart_gia.Series.Count - 1].Color = Color.GreenYellow;
            //chart_gia.Series[chart_gia.Series.Count - 1].BorderDashStyle = ChartDashStyle.DashDotDot;
            //chart_gia.Series[chart_gia.Series.Count - 1].YAxisType = AxisType.Secondary;
            //chart_gia.Series[chart_gia.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(signalLine, soNgayHienThi));
            
             //chart_gia.Series.Add("Xanh Vỏ Đỏ Lòng");
            //chart_gia.Series[chart_gia.Series.Count - 1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
            //chart_gia.Series[chart_gia.Series.Count - 1].BorderWidth = 1;
            ////chart_gia.Series[chart_gia.Series.Count - 1].Color = Color.DimGray;
            //chart_gia.Series[chart_gia.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(XanhVoDoLong, soNgayHienThi));

            //chart_gia.Series.Add("Đỏ vỏ xanh lòng");
            //chart_gia.Series[chart_gia.Series.Count - 1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
            //chart_gia.Series[chart_gia.Series.Count - 1].BorderWidth = 1;
            ////chart_gia.Series[chart_gia.Series.Count - 1].Color = Color.DimGray;
            //chart_gia.Series[chart_gia.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(DoVoXanhLong, soNgayHienThi));

             
            */
            #endregion

            chart_gia.Series.Add("BB Trên");
            chart_gia.Series[chart_gia.Series.Count - 1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
            chart_gia.Series[chart_gia.Series.Count - 1].BorderWidth = 1;
            chart_gia.Series[chart_gia.Series.Count - 1].Color = Color.DimGray;
            chart_gia.Series[chart_gia.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(BB_tren, soNgayHienThi));

            chart_gia.Series.Add("BB Dưới");
            chart_gia.Series[chart_gia.Series.Count - 1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
            chart_gia.Series[chart_gia.Series.Count - 1].BorderWidth = 1;
            chart_gia.Series[chart_gia.Series.Count - 1].Color = Color.DimGray;
            chart_gia.Series[chart_gia.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(BB_duoi, soNgayHienThi));

            chart_gia.Series.Add("EMA " + chuky1.ToString());
            chart_gia.Series[chart_gia.Series.Count - 1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
            chart_gia.Series[chart_gia.Series.Count - 1].BorderWidth = 2;
            chart_gia.Series[chart_gia.Series.Count - 1].Color = Color.YellowGreen;
            chart_gia.Series[chart_gia.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(EMA_5, soNgayHienThi));

            chart_gia.Series.Add("EMA " + chuky2.ToString());
            chart_gia.Series[chart_gia.Series.Count - 1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
            chart_gia.Series[chart_gia.Series.Count - 1].BorderWidth = 2;
            chart_gia.Series[chart_gia.Series.Count - 1].Color = Color.Red;
            chart_gia.Series[chart_gia.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(EMA_10, soNgayHienThi));

            #endregion

            #region Chart_khoiluong
            chart_khoiluong.Series.Clear();
            chart_khoiluong.ChartAreas[0].AxisX.Enabled = AxisEnabled.False;

            chart_khoiluong.Series.Add("Khối lượng");
            chart_khoiluong.Series[chart_khoiluong.Series.Count - 1].Color = Color.Red;
            chart_khoiluong.Series[chart_khoiluong.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(cp_Temp.DanhSachKhoiLuongGiaoDich, soNgayHienThi));

            chart_khoiluong.Series.Add("EMA 15");
            chart_khoiluong.Series[chart_khoiluong.Series.Count - 1].ChartType = SeriesChartType.Spline;
            chart_khoiluong.Series[chart_khoiluong.Series.Count - 1].BorderWidth = 4;
            chart_khoiluong.Series[chart_khoiluong.Series.Count - 1].Color = Color.Blue;
            chart_khoiluong.Series[chart_khoiluong.Series.Count - 1].BorderDashStyle = ChartDashStyle.Dot;
            chart_khoiluong.Series[chart_khoiluong.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(EMA_KhoiLuong, soNgayHienThi));

            chart_khoiluong.Series.Add("Ngày");
            for (int i = 0; i < soNgayHienThi; i++)
            {
                if (i == (trackBar.Value * step))
                    chart_khoiluong.Series[chart_khoiluong.Series.Count - 1].Points.AddY(cp_Temp.DanhSachKhoiLuongGiaoDich[i]);
                else
                    chart_khoiluong.Series[chart_khoiluong.Series.Count - 1].Points.AddY(0);
            }
            #endregion
        }

        private void trackBar_Scroll(object sender, EventArgs e)
        {
            lb_CachHienTai.Text = "Số Ngày Cách Hiện Tại: " + (trackBar.Value * step).ToString();
            Calculate_RSI(trackBar.Value * step);
            Calculate_MFI(trackBar.Value * step);
            Calculate_IMI(trackBar.Value * step);

            //Calculate_Zscore(trackBar.Value * step);

            #region Hiển thị nét gạch từ step ở chart_khoiluong
            chart_khoiluong.Series[chart_khoiluong.Series.Count - 1].Points.Clear();
            for (int i = 0; i < soNgayHienThi; i++)
            {
                if (i == (trackBar.Value * step))
                    chart_khoiluong.Series[chart_khoiluong.Series.Count - 1].Points.AddY(10);
                else
                    chart_khoiluong.Series[chart_khoiluong.Series.Count - 1].Points.AddY(0);
            }
            #endregion

            #region Hiển thị nét gạch từ step ở chart_gia
            List<float> listData = new List<float>();
            foreach (CoPhieu cp in ShareData.danhsachCoPhieu.DanhSachCoPhieu_List)
                if (cp.TenCoPhieu.ToLower() == "^vnindex")
                {
                    listData = cp.DanhSachGiaDongCua;                   
                }

            chart_gia.Series[chart_khoiluong.Series.Count - 1].Points.Clear();
            float min_val = listData[0];
            for (int i = 1; i < soNgayHienThi; i++)
                if (min_val > listData[i])
                    min_val = listData[i];

            for (int i = 0; i < soNgayHienThi; i++)
            {
                if (i == (trackBar.Value * step))
                    chart_gia.Series[chart_khoiluong.Series.Count - 1].Points.AddY(listData[i]);
                else
                    chart_gia.Series[chart_khoiluong.Series.Count - 1].Points.AddY(min_val);
            }
            #endregion
        }


        private void btt_CapNhat_EMA_Click(object sender, EventArgs e)
        {
            int step_EMA = int.Parse(txtbx_StepEMA.Text);
            lstbx_EMA_1.Items.Clear();
            lstbx_EMA_2.Items.Clear();
            for (int i = 5; i <= 100; i += step_EMA)
                lstbx_EMA_1.Items.Add(i.ToString());

            for (int i = 10; i <= 100; i += step_EMA)
                lstbx_EMA_2.Items.Add(i.ToString());           

        }

        private void lstbx_EMA_1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(lstbx_EMA_1.SelectedItem != null)
            {
                int min = int.Parse(lstbx_EMA_1.SelectedItem.ToString());
                int step = int.Parse(txtbx_StepEMA.Text);
                List<float> EMA_1 = Indicators.EMA(CP_ve.DanhSachGiaDongCua, min);
                chart_gia.Series[chart_gia.Series.Count - 2].Name = "EMA " + min;
                chart_gia.Series[chart_gia.Series.Count - 2].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(EMA_1, soNgayHienThi));

                lstbx_EMA_2.Items.Clear();
                for (int i = min + 5; i <= 100; i += step)
                    lstbx_EMA_2.Items.Add(i.ToString());  
            }
        }

        private void lstbx_EMA_2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstbx_EMA_2.SelectedItem != null)
            {
                List<float> EMA_2 = Indicators.EMA(CP_ve.DanhSachGiaDongCua, int.Parse(lstbx_EMA_2.SelectedItem.ToString()));
                chart_gia.Series[chart_gia.Series.Count - 1].Name = "EMA " + int.Parse(lstbx_EMA_2.SelectedItem.ToString());
                chart_gia.Series[chart_gia.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(EMA_2, soNgayHienThi));
            }
        }
    }
}
